package com.fxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxm.constant.RedisConstants;
import com.fxm.domain.User;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.service.StatisticService;
import com.fxm.service.UserService;
import com.fxm.utils.RedisUtil;
import com.fxm.vo.AxisVO;
import com.fxm.vo.result.RewardResult;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 统计模块 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-23
 */
@Slf4j
@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public RespEntity<AxisVO> integral() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDeleted, 0);
        List<User> userList = userService.list(queryWrapper);

        //按照积分 降序排序 取前10
        userList = userList.stream()
                .sorted(Comparator.comparing(User::getIntegral).reversed())
                .limit(10)
                .collect(Collectors.toList());

        //用户昵称集合
        List<String> xAxis = userList.stream()
                .map(User::getUsername)
                .collect(Collectors.toList());

        //用户积分集合
        List<String> yAxis = userList.stream()
                .map(user -> String.valueOf(user.getIntegral()))
                .collect(Collectors.toList());

        return new RespEntity(RespCode.SUCCESS, new AxisVO(xAxis, yAxis));
    }

    @Override
    public RespEntity<RewardResult> sign() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDeleted, 0);
        List<User> userList = userService.list(queryWrapper);

        //所有用户的openid集合
        List<String> openidList = userList.stream()
                .map(User::getOpenid)
                .collect(Collectors.toList());

        Map<String, String> signMap = Maps.newHashMap();
        for (String openid : openidList) {
            String json = redisUtil.get(RedisConstants.key.getSignIn(openid));
            signMap.put(openid, json);
        }

        if (Objects.isNull(signMap) || signMap.isEmpty()) {
            RewardResult rewardResult = RewardResult.builder()
                    .days0(openidList.size()).days1(0).days2(0).days3(0)
                    .days4(0).days5(0).days6(0).days7(0).build();
            return new RespEntity(RespCode.SUCCESS, rewardResult);
        }

        //累计签到天数集合
        List<String> day1List = Lists.newArrayList();
        List<String> day2List = Lists.newArrayList();
        List<String> day3List = Lists.newArrayList();
        List<String> day4List = Lists.newArrayList();
        List<String> day5List = Lists.newArrayList();
        List<String> day6List = Lists.newArrayList();
        List<String> day7List = Lists.newArrayList();

        for (Map.Entry<String, String> entry : signMap.entrySet()) {
            switch (entry.getValue()) {
                case "1" : day1List.add(entry.getKey()); break;
                case "2" : day2List.add(entry.getKey()); break;
                case "3" : day3List.add(entry.getKey()); break;
                case "4" : day4List.add(entry.getKey()); break;
                case "5" : day5List.add(entry.getKey()); break;
                case "6" : day6List.add(entry.getKey()); break;
                default: day7List.add(entry.getKey());
            }
        }

        //七个集合 取并集
        List<String> daysAllList = Lists.newArrayList();
        daysAllList.addAll(day1List);
        daysAllList.addAll(day2List);
        daysAllList.addAll(day3List);
        daysAllList.addAll(day4List);
        daysAllList.addAll(day5List);
        daysAllList.addAll(day6List);
        daysAllList.addAll(day7List);

        //两个集合 取差集
        openidList.removeAll(daysAllList);

        //数据返回封装
        RewardResult rewardResult = RewardResult.builder()
                .days0(openidList.size())
                .days1(day1List.size())
                .days2(day2List.size())
                .days3(day3List.size())
                .days4(day4List.size())
                .days5(day5List.size())
                .days6(day6List.size())
                .days7(day7List.size())
                .build();

        return new RespEntity(RespCode.SUCCESS, rewardResult);
    }

}
